package com.example.baidu;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;


import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private MapView mMapView = null;
    private BaiduMap mBaiduMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMapView = (MapView) findViewById(R.id.bmapView);
        Log.e("sss", "加载完成");
        mBaiduMap=mMapView.getMap();
        //开启定位图层
        mMapView.getMap().setMyLocationEnabled(true);
        mMapView.showZoomControls(true);

                //构建折线点坐标
                LatLng p1 = new LatLng(39.97923, 116.357428);
                LatLng p2 = new LatLng(39.94923, 116.397428);
                LatLng p3 = new LatLng(39.97923, 116.437428);
                List<LatLng> points = new ArrayList<LatLng>();
                points.add(p1);
                points.add(p2);
                points.add(p3);

//设置折线的属性
                OverlayOptions mOverlayOptions = new PolylineOptions()
                        .width(10)
                        .color(0xAAFF0000)
                        .points(points);
//在地图上绘制折线
//mPloyline 折线对象
                mBaiduMap.addOverlay(mOverlayOptions);

        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Log.e("点击",latLng.toString());
            }

            @Override
            public void onMapPoiClick(MapPoi mapPoi) {
                Log.e("点击",mapPoi.getName());
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("提示")
                        .setMessage(mapPoi.getName())
                        .create()
                        .show();

            }
        });

    }



    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBaiduMap.setMyLocationEnabled(false);
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }
}